package com.test.service;

import com.test.dao.ChoiceDao;
import com.test.dao.ChoiceFullDao;
import com.test.dao.DishDao;
import com.test.dao.MerchantDao;
import com.test.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

@Component
public class ChoiceService {

    private final ChoiceDao choiceDao;

    private final MerchantDao merchantDao;

    private final DishDao dishDao;

    private final ChoiceFullDao choiceFullDao;

    @Autowired
    public ChoiceService(ChoiceDao choiceDao, MerchantDao merchantDao, DishDao dishDao, ChoiceFullDao choiceFullDao) {
        this.choiceDao = choiceDao;
        this.merchantDao = merchantDao;
        this.dishDao = dishDao;
        this.choiceFullDao = choiceFullDao;
    }

    public void chooseDish(String customerPhoneNumber, String dishNumber) {

        Integer isExist = choiceDao.getCountByCustomerPhoneNumberAndDishNumber(customerPhoneNumber, dishNumber);

        System.out.println(isExist);
        if(isExist.equals(0)){//如果没有添加过，则插入
            choiceDao.insertChoice(customerPhoneNumber, dishNumber);
        }
        else {//添加过则数量+1
            choiceDao.updateChoiceDishNum(customerPhoneNumber, dishNumber);
        }

    }


    public List<ChoiceFront> getChoiceList(String customerPhoneNumber){

        List<ChoiceFront> choiceFrontList=new ArrayList<>();
        List<String> allMerchantPhoneNumber = choiceFullDao.getAllMerchantPhoneNumberByCustomerPhoneNumber(customerPhoneNumber);
        System.out.println(allMerchantPhoneNumber);
        List<ChoiceFull> allChoice = choiceFullDao.getAllChoiceFullByCustomerPhoneNumber(customerPhoneNumber);
        System.out.println(allChoice);
        for (String merchantPhoneNumber : allMerchantPhoneNumber) {
            Merchant merchant=merchantDao.getMerchantByPhoneNumber(merchantPhoneNumber);
            List<ChoiceFull> collectChoiceList=new ArrayList<>();
            for (ChoiceFull choiceFull : allChoice) {
                if (Objects.equals(choiceFull.getMerchantPhoneNumber(), merchantPhoneNumber))
                    collectChoiceList.add(choiceFull);
            }
            choiceFrontList.add(new ChoiceFront(merchant,collectChoiceList));

        }


        return choiceFrontList;
    }

    public void subNum(String customerPhoneNumber,String dishNumber){
        choiceDao.updateChoiceDishNumS(customerPhoneNumber,dishNumber);
    }

    public void deleteChoice(String customerPhoneNumber,String dishNumber){
        choiceDao.deleteChoiceByDishNumberAndCustomerPhoneNumber(dishNumber,customerPhoneNumber);
    }

    public void createOrder(BigDecimal wholePrice,String accountNumber,String phoneNumber,String allAddress,List<String> dishList) throws InterruptedException {

        Set<String> allMerchant = new HashSet<>();
        for (String dishNumber : dishList) {
            allMerchant.add(dishDao.getMerchantPhoneByDishNumber(dishNumber));
        }
        System.out.println(allMerchant);

        for (String merchantPhoneNumber : allMerchant) {
            String orderNumber="od"+System.currentTimeMillis();
            for (String dishNumber : dishList) {
                String temp=dishDao.getMerchantPhoneByDishNumber(dishNumber);
                if (temp.equals(merchantPhoneNumber)){
                    choiceDao.createOrder(accountNumber,allAddress,dishNumber,orderNumber);
                }
            }
            Thread.currentThread().sleep(1);
        }

        for (String dishNumber : dishList) {

            choiceDao.deleteChoiceByDishNumberAndCustomerPhoneNumber(dishNumber,phoneNumber);
        }

//
    }

}
